package com.api.shopapi.controller;

import com.alibaba.fastjson.JSONObject;
import com.api.shopapi.entity.*;
import com.api.shopapi.entity.dto.ArticleDTO;
import com.api.shopapi.service.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.UUID;

/**
 * 文章
 * */

@RequestMapping("Article")
@RestController
public class ArticleController extends BaseController {

    @Autowired
    @Qualifier("ArticleServiceImplOne")
    private ArticleService articleService;

    @Autowired
    @Qualifier("StaffServiceImplOne")
    private StaffService staffService;

    @Autowired
    @Qualifier("ArticleClassServiceImplOne")
    private ArticleClassService articleClassService;

    @Autowired
    @Qualifier("UserServiceImplOne")
    private UserService userService;


    @RequestMapping(value = "Add",method = RequestMethod.POST)
    public MyResult Add(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String articleClassCode = jsonObject.getString("articleClassCode");
            String title =jsonObject.getString("title");
            String description=jsonObject.getString("description");
            String content =jsonObject.getString("content");
            //Date startTime = jsonObject.getDate("startTime");
            //Date endTime = jsonObject.getDate("endTime");
            String fileUrl1=jsonObject.getString("fileUrl1");
            String fileUrl2=jsonObject.getString("fileUrl2");
            String fileUrl3=jsonObject.getString("fileUrl3");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/Article/Add");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff =staffService.getByStaffCode(staffCode);

            ArticleClass articleClass = articleClassService.getByArticleClassCode(articleClassCode);

            String articleCode = UUID.randomUUID().toString();


            Article article =new Article();
            article.setArticleCode(articleCode);
            article.setArticleClassCode(articleClassCode);
            article.setArticleClassName(articleClass.getName());
            article.setTitle(title);
            article.setDescription(description);
            article.setContent(content);
            article.setCreateTime(new Date());
            article.setStatus(1);
            //article.setStaffCode(staffCode);
            article.setGovernmentCode(staff.getGovernmentCode());
            article.setStartTime( new Date());
            article.setEndTime(new Date());
            article.setFileUrl1(fileUrl1);
            article.setFileUrl2(fileUrl2);
            article.setFileUrl3(fileUrl3);
            //article.setJoinPeople(0);

            articleService.insert(article);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/Article/Add", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "Edit",method = RequestMethod.POST)
    public MyResult Edit(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String title =jsonObject.getString("title");
            String description=jsonObject.getString("description");
            String content =jsonObject.getString("content");
            String articleCode=jsonObject.getString("articleCode");
            String articleClassCode = jsonObject.getString("articleClassCode");
            int status = jsonObject.getIntValue("status");
            //Date startTime = jsonObject.getDate("startTime");
            //Date endTime = jsonObject.getDate("endTime");
            String fileUrl1=jsonObject.getString("fileUrl1");
            String fileUrl2=jsonObject.getString("fileUrl2");
            String fileUrl3=jsonObject.getString("fileUrl3");

            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/Article/Edit");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            Article article =articleService.getByArticleCode(articleCode);
            if(article==null){
                return MyResult.Failed("该活动不存在！");
            }

            ArticleClass articleClass = articleClassService.getByArticleClassCode(articleClassCode);
            article.setArticleClassCode(articleClassCode);
            article.setArticleClassName(articleClass.getName());
            article.setTitle(title);
            article.setDescription(description);
            article.setContent(content);
            article.setStatus(status);
            //article.setStartTime(startTime);
            //article.setEndTime(endTime);
            article.setFileUrl1(fileUrl1);
            article.setFileUrl2(fileUrl2);
            article.setFileUrl3(fileUrl3);
            articleService.update(article);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);
        } catch (Exception e) {
            insertError("/Article/Edit", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    //预览
    @RequestMapping(value = "Preview",method = RequestMethod.POST)
    public MyResult Preview(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");

            String articleCode=jsonObject.getString("articleCode");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/Article/Preview");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Article article =articleService.getByArticleCode(articleCode);
            return success(article);
        } catch (Exception e) {
            insertError("/Article/Preview", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "Get",method = RequestMethod.POST)
    public MyResult Get(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String articleCode=jsonObject.getString("articleCode");


            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Article/Preview");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Article article =articleService.getByArticleCode(articleCode);

            return success(article);
        } catch (Exception e) {
            insertError("/Article/Preview", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    //列表
    @RequestMapping(value = "Page",method = RequestMethod.POST)
    public MyResult Page(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");
            String title = jsonObject.getString("title");
            int status = jsonObject.getIntValue("status");
            String startTime = jsonObject.getString("startTime");
            String endTime = jsonObject.getString("endTime");
            endTime=endTime+" 23:59:59";
            String networkCode = jsonObject.getString("networkCode");
            String communityCode = jsonObject.getString("communityCode");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/Article/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            Staff staff =staffService.getByStaffCode(staffCode);

            PageInfo<ArticleDTO> articlePageInfo =
                    articleService.getPageList(pageIndex,pageSize,staff.getGovernmentCode(),title,status,startTime,endTime,communityCode,networkCode);

            return success(articlePageInfo);
        } catch (Exception e) {
            insertError("/Article/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }

    @RequestMapping(value = "GovPageApp",method = RequestMethod.POST)
    public MyResult GovPageApp(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String userCode = jsonObject.getString("userCode");

            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");

            PreCheck preCheck = checkSignFrontInterface(timeStr, sign,"/Article/Page");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }

            User user = userService.getByUserCode(userCode);

            PageInfo<ArticleDTO> articlePageInfo =
                    articleService.pageByGovCode(pageIndex,pageSize,user.getGovernmentCode());

            return success(articlePageInfo);
        } catch (Exception e) {
            insertError("/Article/Page", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }


    @RequestMapping(value = "Revocation",method = RequestMethod.POST)
    public MyResult Revocation(@RequestBody JSONObject jsonObject){
        try {
            String timeStr = jsonObject.getString("timeStr");
            String sign = jsonObject.getString("sign");
            String staffCode = jsonObject.getString("staffCode");
            String articleCode=jsonObject.getString("articleCode");
            PreCheck preCheck = checkSignAdmInterface(timeStr, sign,staffCode,"/Article/Revocation");
            if (preCheck.getError() == 1) {
                return failed(preCheck.getMessage());
            }
            Article article =articleService.getByArticleCode(articleCode);
            article.setStatus(2);
            articleService.update(article);

            JSONObject jsonObject1 =new JSONObject();
            return success(jsonObject1);

        } catch (Exception e) {
            insertError("/Article/Revocation", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }



    //列表
    @RequestMapping(value = "PageToGovPlatform",method = RequestMethod.POST)
    public MyResult PageToGovPlatform(@RequestBody JSONObject jsonObject){
        try {

            int pageIndex =jsonObject.getInteger("pageIndex");
            int pageSize =jsonObject.getInteger("pageSize");
            String title = jsonObject.getString("title");
            int status = jsonObject.getIntValue("status");
            String startTime = jsonObject.getString("startTime");
            String endTime = jsonObject.getString("endTime");
            endTime=endTime+" 23:59:59";
            String networkCode = jsonObject.getString("networkCode");
            String communityCode = jsonObject.getString("communityCode");
            String governmentCode = jsonObject.getString("governmentCode");



            PageInfo<ArticleDTO> articlePageInfo =
                    articleService.getPageList(pageIndex,pageSize,governmentCode,title,status,startTime,endTime,communityCode,networkCode);

            return success(articlePageInfo);
        } catch (Exception e) {
            insertError("/Article/PageToGovPlatform", jsonObject.toJSONString(), getExceptionMsg(e), "api");
            return anomalous(getExceptionMsg(e));
        }
    }






}
